<?php
namespace Home\Controller;

class UserController extends \Think\Controller{
    
    public $fromuser; //当前用户openid
    
    public function _initialize(){
        header("Content-Type: text/html; charset=utf-8");
        
        //获取用户fromuser
        $this->fromuser = session('fromuser');
        if($_REQUEST['code'] && $this->fromuser==''){
            $this->fromuser = D('WxApi')->getAuthOpenid($_REQUEST['code'],C('APPID'),C('APPSECRET'));
            if(empty($this->fromuser)){
                die('<script>alert("获取用户标识失败")</script>');
            }else{
                session('fromuser',$this->fromuser);
            }
        }elseif($this->fromuser==''){
            $redirectUri="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];//openid处理页面(当前页面)
            D('WxApi')->getAuthCode($redirectUri,C('APPID'));
        }
    }
    
    public function phone(){
        $this->assign('fromuser',$this->fromuser);
		$this->display();
	}
    
    public function regPhone(){
        $User = M('User');
		$data = I('post.');
        
        //手机验证码核对
        if($data['phone'] != session('phone') or empty($data['phone'])){
            echo json_encode(array('status'=>0,'msg'=>'手机号错误'));die;
        }
        if($data['code'] != session('mobile_code')or empty($data['code'])){
            echo json_encode(array('status'=>0,'msg'=>'验证码错误'));die;
        }
        session('phone',null);
        session('mobile_code',null);
        
        if($row = $User->where(array('fromuser'=>$data['fromuser']))->find()){
            echo json_encode(array('status'=>0,'msg'=>'该微信号已被关联过'));die;
        }
        
        //数据保存
        $phoneRow = $User->where(array('phone'=>$data['phone']))->find();
        if($phoneRow['id']){//被医生添加过或为后台添加的医生或营养师
            $dat = array('id'=>$phoneRow['id'],'phone'=>$data['phone'],'fromuser'=>$data['fromuser']);
            $res = $User->save($dat);
            $userId = $phoneRow['id'];
        }else{//患者自己添加
            $res = $User->add(array('type'=>'2','phone'=>$data['phone'],'fromuser'=>$data['fromuser']));
            $userId = $res;
        }
        
        D('User')->changeUserScore($userId,100,'reg');//积分-增加
        
        echo json_encode(array('status'=>1,'msg'=>'关联成功','type'=>$phoneRow['type']));die;
	}
    
    //资料完善
    public function complete(){
        if(IS_POST){
            $res = M('User')->save(I('post.'));
            $this->ajaxReturn('suc');
        }else{
            //当前用户信息
            $userInfo = M('User')->where(array('fromuser'=>$this->fromuser))->find();
            if(empty($userInfo)) $this->redirect('User/phone');
            
            if($userInfo['city_id']) $cRow = M('HptCity')->find($userInfo['city_id']);//城市名
            if($userInfo['hpt_id']) $hptRow = M('Hospital')->find($userInfo['hpt_id']);//医院名
            $userInfo['city_name'] = $cRow['name'];
            $userInfo['hpt_name'] = $hptRow['name'];
            $this->assign('vo',$userInfo);
            
            $this->assign('uid',$userInfo['id']);//用户id
            $this->assign('cityArr',M('HptCity')->order('id asc')->select());//获取城市
            $this->display();
        }
	}
    
    //上传身份证照
    public function idcard(){
        if(IS_POST){
            $res = M('User')->save(I('post.'));
            $this->ajaxReturn('suc');
        }else{
            //当前用户信息
            $userInfo = M('User')->where(array('fromuser'=>$this->fromuser))->find();
            if(empty($userInfo)) $this->redirect('User/phone');
            
            $this->assign('vo',$userInfo);
            $this->assign('uid',$userInfo['id']);//用户id
            $this->display();
        }
	}
}